﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Nirvana
{
    public abstract class Translatable : MonoBehaviour
    {

        /// <summary>
        /// 语言表ID
        /// </summary>
        [Tooltip("The text for keyid.")]
        [SerializeField]
        private string keyid;
        public string KeyId { get { return keyid; }}

        [Tooltip("以string.format(\"{0} newchar\",keyid)输出字符")]
        [SerializeField]
        private string format;
        /// <summary>
        /// keyid含有占位符时使用。当有多个占位符时:{0}{1}{n},inputvalue值需以逗号","分割
        /// </summary>
        [Tooltip("以string.format(\"keyid \",placeholder)输出字符,keyid必须带{0}等占位符")]
        [SerializeField]
        private string Placeholder;
        /// <summary>
        /// 当语言切换时，上报脚本对象处理，用于脚本控制的翻译能够实时切换
        /// </summary>
        [Serializable]
        public class OnLanguageEvent : UnityEvent { }
        [SerializeField]
        private OnLanguageEvent onLanguageChange;

        protected I18NLanguageType languageType = I18NLanguageType.Unknown;

#if UNITY_EDITOR
        /// <summary>
        /// 为测试
        /// </summary>
        [SerializeField]
        protected I18NLanguageType TranslateLanguage = I18NLanguageType.Unknown;
#endif

        /// <summary>
        /// 字体切换回调
        /// </summary>
        protected abstract void TranslateText();
        private LinkedListNode<Action> eventlist;

        protected virtual void Awake()
        {
            if (this.languageType != I18N.Language)
            {
                this.languageType = I18N.Language;
                this.TranslateText();
            }
        }

#if UNITY_EDITOR
        private void OnValidate()
        {
            if (!string.IsNullOrEmpty(this.format))
            {
                this.format = this.format.Replace("\r\n", "\n").Replace("\n\r", "\n");
            }
            this.TranslateText();
        }
#endif


        private void OnEnable()
        {
            this.eventlist = I18N.ListenLanguageChanged(this.OnLanguageChange);
            this.OnLanguageChange();
        }

        private void OnDisable()
        {
            if (this.eventlist != null)
            {
                I18N.UnlistenLanguageChanged(this.eventlist);
                this.eventlist = null;
            }
        }

        private void OnLanguageChange()
        {
            if(this.languageType != I18N.Language)
            {
                this.languageType = I18N.Language;
                this.TranslateText();
                if (Application.isPlaying)
                {
                    if (onLanguageChange != null) onLanguageChange.Invoke();
                }
            }
        }

        public void SetKeyId(string key)
        {
            keyid = key;
        }

        /// <summary>
        /// For Editor Test
        /// </summary>
        public void TestSwitchLanugage(int language)
        {
            var languageType = (I18NLanguageType)language;
            if (languageType == I18NLanguageType.Unknown) return;
            I18N.Language = languageType;
        }

        public virtual void GetTranslate(out string txt)
        {
            txt = I18N.GetText(this.keyid, "");
            try
            {
                if (!string.IsNullOrEmpty(this.format))
                {
                    txt = string.Format(format, txt);
                }
                if (!string.IsNullOrEmpty(this.Placeholder))
                {
                    txt = string.Format(txt, Placeholder);
                } 
            } 
            catch (Exception ex)
            {
                Debug.LogError("I18N Text Error:" + ex.Message);
            }

        }
        public virtual void GetTranslate(out Sprite img)
        {
            img = I18N.GetSprite(this.keyid, "");
        }



    }
}

